package com.xzm.project.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xzm.project.R;

/**
 * author : xuzongmeng
 * date   : 2019/9/5
 * desc   : 动态帧布局
 */
public class DynamicFrameLayoutActivity extends AppCompatActivity {
    private LinearLayout rootView;
    private Button btnTest1;
    private Button btnTest2;
    private Button btnTest3;
    private Button btnTest4;
    private Button btnTest5;
    private Button btnTest6;
    private Button btnTest7;
    private Button btnTest8;
    private Button btnTest9;
    public Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
//        setContentView(R.layout.activity_dynamic_relativelayout);
        setContentView(getLayoutRootView());
        initView(savedInstanceState);
        initListener();
    }
    /**
     * 视图根容器
     */
    private View getLayoutRootView() {
        rootView = new LinearLayout(mContext);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        rootView.setLayoutParams(layoutParams);
        return rootView;
    }
    /**
     * 初始化Params
     */
    private FrameLayout.LayoutParams getFrameLayoutParams() {
        FrameLayout.LayoutParams params = null;
        if (params == null) {
            params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return params;
    }

    protected void initView(Bundle savedInstanceState) {
        //第一个子布局容器
        FrameLayout childContainer1 = new FrameLayout(mContext);
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
        layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP); // 这句不写也默认 第一个子布局容器  也在第一个
        layoutParams1.setMarginStart(20);
        childContainer1.setPadding(0,0,50,0);
        childContainer1.setBackgroundColor(Color.RED);
        childContainer1.setId(R.id.childContainer1);
        childContainer1.setLayoutParams(layoutParams1);



        btnTest1 = new Button(mContext);
        btnTest1.setId(R.id.btnTest1);
        btnTest1.setText("按钮1");

        btnTest2 = new Button(mContext);
        btnTest2.setId(R.id.btnTest2);
        btnTest2.setText("按钮2");

        btnTest3 = new Button(mContext);
        btnTest3.setId(R.id.btnTest3);
        btnTest3.setText("按钮3");

        FrameLayout.LayoutParams param1 = getFrameLayoutParams();
        FrameLayout.LayoutParams param2 = getFrameLayoutParams();
        FrameLayout.LayoutParams param3 = getFrameLayoutParams();



        btnTest1.setLayoutParams(param1);
        btnTest2.setLayoutParams(param2);
        btnTest3.setLayoutParams(param3);


        childContainer1.addView(btnTest1);
        childContainer1.addView(btnTest2);
        childContainer1.addView(btnTest3);

        rootView.addView(childContainer1);
        //第二个子布局容器
        FrameLayout childContainer2 = new FrameLayout(mContext);
        FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        childContainer2.setBackgroundColor(Color.GREEN);
        childContainer2.setId(R.id.childContainer2);
        childContainer2.setLayoutParams(layoutParams2);


        btnTest4 = new Button(mContext);
        btnTest4.setId(R.id.btnTest4);
        btnTest4.setText("按钮4");

        btnTest5 = new Button(mContext);
        btnTest5.setId(R.id.btnTest5);
        btnTest5.setText("按钮5");

        btnTest6 = new Button(mContext);
        btnTest6.setId(R.id.btnTest6);
        btnTest6.setText("按钮6");



        FrameLayout.LayoutParams param4 = getFrameLayoutParams();
        FrameLayout.LayoutParams param5 = getFrameLayoutParams();
        FrameLayout.LayoutParams param6 = getFrameLayoutParams();


        btnTest4.setLayoutParams(param4);
        btnTest5.setLayoutParams(param5);
        btnTest6.setLayoutParams(param6);

        childContainer2.addView(btnTest4);
        childContainer2.addView(btnTest5);
        childContainer2.addView(btnTest6);

        rootView.addView(childContainer2);







        //第三个子布局容器
        FrameLayout childContainer3 = new FrameLayout(mContext);
        FrameLayout.LayoutParams layoutParams3 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                300);
        childContainer3.setBackgroundColor(Color.BLUE);
        childContainer3.setId(R.id.childContainer3);
        childContainer3.setLayoutParams(layoutParams3);


        btnTest7 = new Button(mContext);
        btnTest7.setId(R.id.btnTest7);
        btnTest7.setText("按钮7");

        btnTest8 = new Button(mContext);
        btnTest8.setId(R.id.btnTest8);
        btnTest8.setText("按钮8");

        btnTest9 = new Button(mContext);
        btnTest9.setId(R.id.btnTest9);
        btnTest9.setText("按钮9");



        FrameLayout.LayoutParams param7 = getFrameLayoutParams();
        FrameLayout.LayoutParams param8 = getFrameLayoutParams();
        FrameLayout.LayoutParams param9 = getFrameLayoutParams();

        btnTest7.setLayoutParams(param7);
        btnTest8.setLayoutParams(param8);
        btnTest9.setLayoutParams(param9);

        childContainer3.addView(btnTest7);
        childContainer3.addView(btnTest8);
        childContainer3.addView(btnTest9);

        rootView.addView(childContainer3);

    }

    protected void initListener() {
        btnTest1.setOnClickListener(v -> Toast.makeText(mContext, "btnTest1", Toast.LENGTH_SHORT).show());
        btnTest2.setOnClickListener(v -> Toast.makeText(mContext, "btnTest2", Toast.LENGTH_SHORT).show());
        btnTest3.setOnClickListener(v -> Toast.makeText(mContext, "btnTest3", Toast.LENGTH_SHORT).show());
        btnTest4.setOnClickListener(v -> Toast.makeText(mContext, "btnTest4", Toast.LENGTH_SHORT).show());
        btnTest5.setOnClickListener(v -> Toast.makeText(mContext, "btnTest5", Toast.LENGTH_SHORT).show());
        btnTest6.setOnClickListener(v -> Toast.makeText(mContext, "btnTest6", Toast.LENGTH_SHORT).show());
    }
}
